package com.xuxk.project.oauth.controller.rpc;

import com.github.pagehelper.Page;
import com.xuxk.project.oauth.annotation.OauthLog;
import com.xuxk.project.oauth.bean.UserRole;
import com.xuxk.project.oauth.service.UserRoleService;
import com.xuxk.project.oauth.util.PageInfoVo;
import com.xuxk.project.oauth.util.PageUtil;
import com.xuxk.project.oauth.util.RequestUtils;
import com.xuxk.project.oauth.util.ResultBodyVo;
import com.xuxk.project.oauth.vo.UserRoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
 *   @author  橙云科技有限公司
 *  只对内部服务开放，不对第三方开放
 */

@RequestMapping("/rpc/userRole")
@RestController
public class UserRoleController {

    @Autowired
	UserRoleService userRoleService;


    /**
    * 分页查询
    * @param pageNum  页码
    * @param pageSize 页行数
    * @param request
    * 资源标识符： userRole:userRoles
    * @return
    */
    @GetMapping(value = "/v1.0/userRoles", produces = MediaType.APPLICATION_JSON_VALUE)
    @OauthLog(eventName = "分页查询用户角色对应关系列表")
	public PageInfoVo userRoles(Integer pageNum, Integer pageSize, HttpServletRequest request){

        Map<String,Object> paramMap = RequestUtils.getQueryParams(request);

        Page<UserRoleVo> pageResult = userRoleService.listUserRoles(paramMap, PageUtil.getPageParam(pageNum,pageSize));

		return new PageInfoVo(pageResult);
	}



    @GetMapping(value="/v1.0/userRoles/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @OauthLog(eventName = "查询用户角色关系")
    public UserRole getUserRole(@PathVariable Integer id) {
        UserRole userRole = userRoleService.getUserRole(id );

        return userRole;
    }


    @PostMapping(value="/v1.0/userRoles", produces = MediaType.APPLICATION_JSON_VALUE)
    @OauthLog(eventName = "新增用户角色关系")
    public Integer saveUserRole(@RequestBody UserRole userRole) {

        return  userRoleService.insertUserRole(userRole);

    }

    /**
     * 部分更新
     * @param userRole
     * @return
     */
    @PatchMapping(value="/v1.0/userRoles/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public Integer patchUpdateUserRole(@PathVariable Integer id,@RequestBody UserRole userRole) {
        userRole.setId(id);

        return userRoleService.patchUpdateUserRole(userRole);


    }
    /**
     * 整体更新
     * @param userRole

     * @return
     */
    @PutMapping(value="/v1.0/userRoles/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public Integer updateUserRole(@PathVariable Integer id,@RequestBody UserRole userRole) {

        userRole.setId(id);

        return  userRoleService.updateUserRole(userRole);
    }

    @DeleteMapping(value="/v1.0/userRoles/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @OauthLog(eventName = "删除用户角色关系")
    public Integer deleteUserRole(@PathVariable Integer id) {

        userRoleService.deleteUserRole(id );

        return 1;

    }

}
